//
//  ProductIntroductViewController.m
//  LazyCat
//
//  Created by ╰莪呮想好好宠Nǐつ on 16/4/30.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "ProductIntroductViewController.h"
#import "SZTextView.h" //可以设置占位符颜色的textview
#import <QuartzCore/QuartzCore.h>

@interface ProductIntroductViewController ()<UITextViewDelegate>

@property(nonatomic,strong)SZTextView *textView;
@property(nonatomic,strong)UIButton *saveBt;

@end

@implementation ProductIntroductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardshow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardhide:) name:UIKeyboardWillHideNotification object:nil];
    
}
#pragma mark 当键盘出现或改变时调用
-(void)keyBoardshow:(NSNotification *)notification{
    NSDictionary *info=[notification userInfo];
    NSValue *avalue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect=[self.view convertRect:[avalue CGRectValue] fromView:nil];
    double keyboardHeight=keyboardRect.size.height;//键盘的高度
    [UIView animateWithDuration:0.2 animations:^{
        //最后多减20，是为了留出更多地方点击回收键盘
        self.textView.frame = CGRectMake(10, 74, ScreenWidth-20, ScreenHeight-keyboardHeight-45-74-20);
        self.saveBt.frame = CGRectMake(0,ScreenHeight-45-keyboardHeight, ScreenWidth, 45);
    }];
    
}
-(void)keyBoardhide:(NSNotification *)notification{
    [UIView animateWithDuration:0.2 animations:^{
        self.textView.frame = CGRectMake(10, 74, ScreenWidth-20, ScreenHeight-45-74-40);
        self.saveBt.frame = CGRectMake(0,ScreenHeight-45, ScreenWidth, 45);
    }];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
-(void)toHideKeyboard
{
    [self.view endEditing:YES];
}
#pragma mark --- 创建视图
-(void)creatUI
{
    [self initNav:@"商品简介" doBack:YES];
    //给导航栏添加点击事件，隐藏键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toHideKeyboard)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.navBar addGestureRecognizer:tapGestureRecognizer];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [self.navBar addSubview:lineView];
    self.view.backgroundColor = SetColorRGBValue(0xF9F9F9, 1.0);
    
    self.textView = [[SZTextView alloc]initWithFrame:CGRectMake(10, 74, ScreenWidth-20, ScreenHeight-74-45)];
    self.textView.textColor = KAddLeftLbColor;
    self.textView.placeholderTextColor = KPlaceholderColor;
    self.textView.placeholder = @"请输入商品简介";
    self.textView.text = self.lc_textIntroStr;
    self.textView.delegate = self;
    self.textView.backgroundColor = SetColorRGBValue(0xF9F9F9, 1.0);
    [self.view addSubview:self.textView];
    self.textView.font = FontSet(15);
    
    //一进来就可以输入
    [self.textView becomeFirstResponder];
    
    UIButton *saveBt = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBt.frame = CGRectMake(0,ScreenHeight-45, ScreenWidth, 45);
    saveBt.backgroundColor = SetColor(255, 100, 37, 1.0);
    [saveBt setTitle:@"保存" forState:UIControlStateNormal];
    [saveBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:saveBt];
    self.saveBt = saveBt;
    
    [saveBt addTarget:self action:@selector(saveBtClicked) forControlEvents:UIControlEventTouchUpInside];
    
    //修改baseVC里设置的方法
    [self.backBtn removeTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.backBtn addTarget:self action:@selector(isToBack) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backAction:(UIButton *)btn{
    
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --- "保存"点击事件
-(void)saveBtClicked
{
    [self.view endEditing:YES];
    
    if ([self.textView.text isEqualToString:@""]) {
        [[MessShowView share] showWithMess:@"请输入产品简介" login:NO];
        return;
    }
    if (self.overTextIntroBlock) {
        self.overTextIntroBlock(self.textView.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)isToBack
{
   
//    if (self.overTextIntroBlock) {
//        self.overTextIntroBlock(self.textView.text);
//    }
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)textViewDidChange:(UITextView *)textView {
    
    if (textView.text.length>200) {
        [[MessShowView share] showWithMess:@"最多请输入200字"];
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 199)];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
